package com.example.cms.config;

import com.example.common.Interceptor.AuthInterceptor;
import com.example.common.properties.CorsProperties;
import com.example.common.properties.JwtProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Auther: hjy(lc))
 * @Date: 2022/11/2-11-02-11:03
 * @Description：com.example.common.config    === SpringMvc.xml  (拦截器、控制器、映射器)
 */
@Configuration
public class MvcConfig implements WebMvcConfigurer {
    /**
     * 添加拦截器
     * @param registry  拦截器注册器
     */
    @Autowired
    private JwtProperties jwtProperties;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new AuthInterceptor())
                .addPathPatterns(jwtProperties.getPathPattern());

    }
//@EnableWebMvc
    @Autowired
    private CorsProperties corsProperty;
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping(corsProperty.getPathPattern())//映射所有路径
                // * 允许所有的客户端携带脚本请求 此处应该只配置前端的服务器
                .allowedOrigins(corsProperty.getOrigins())//运行所有客户端访问(核心) Access-Control-Allow-Origin
                .allowCredentials(corsProperty.isAllowCredentials())//不允许携带cookie Access-Control-Allow-Credentials
                // OPTIONS 预见请求
                .allowedMethods(corsProperty.getMethods())//支持的方法 Access-Control-Allow-Methods
                .allowedHeaders(corsProperty.getHeaders())//运行所有请求头字段
                .maxAge(corsProperty.getMaxAge());//允许客户端缓存“预检请求”中获取的信息，3600秒
    }

}
